﻿using ASmile.Extern.Lib.HttpCore;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ASmile.Extern.Lib
{
    /// <summary>
    /// 三方数据接口
    /// </summary>
    public class DataInterface
    {
        static DataInterface()
        {
            Http = new HttpClient();
            //Http.ThrowException = false;
        }

        static HttpClient Http { get; }

        public static bool GetData(string url, string type, object data, out string result)
        {
            url = $"{url}?type={type}";
            string body = JsonHelper.ToJson(data);
            var ret = Http.Post(url, body, out result);
            if (ret != 200)
            {
                return false;
            }
            return true;
        }

        public static bool Test(string url)
        {
            return GetData(url, "TEST", null, out _);
        }

        public static TRet GetData<TRet>(string url, string type, object data)
        {
            url = $"{url}?type={type}";
            string body = JsonHelper.ToJson(data);
            if (!GetData(url, type, body, out string result))
            {
                throw new Exception($"请求外部接口失败, {result}");
            }
            var ret = JsonHelper.JsonToObject<TRet>(result);
            return ret;
        }

        public static ApiResult GetData(string url, string type, object data)
        {
            ApiResult rData;
            url = $"{url}?type={type}";
            string body = JsonHelper.ToJson(data);
            var ret = Http.Post(url, body, out string result);
            if (ret != 200)
            {
                rData = new ApiResult
                {
                    code = -1,
                    msg = $"发生错误，{result}"
                };
                return rData;
            }
            rData = JsonHelper.JsonToObject<ApiResult>(result);
            return rData;
        }

        public static ApiResult GetData(string type, object data)
        {
            string url = Config.Settings.Url;
            return GetData(url, type, data);
        }

        public static ApiResult GetData(string type)
        {
            string url = Config.Settings.Url;
            return GetData(url, type, null);
        }

        /// <summary>
        /// 异步获取数据，当 委托 OnResultAction 返回 false 退出改方法，否则一直获取
        /// </summary>
        public static void GetDataAsync(string url, string type, object data, Func<ApiResult, bool> OnResultAction)
        {
            if (OnResultAction == null) return;
            Task.Factory.StartNew(GetResult);
            void GetResult()
            {
                while (true)
                {
                    var ret = GetData(url, type, data);
                    if (ret.success)
                    {
                        if (!OnResultAction(ret)) return;
                    }
                }
            }
        }

        public static void GetDataAsync(string url, string type, Func<ApiResult, bool> OnResultAction)
        {
            GetDataAsync(url, type, null, OnResultAction);
        }
    }
}